package toubiao.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import toubiao.domain.vo.assist.Json;
import toubiao.domain.vo.manage.User;
import toubiao.exception.CustomException;
import toubiao.service.UserServiceI;
import toubiao.utils.ProUtilConfig;

@Controller
@RequestMapping("/login")
public class LoginController {

	private static Logger logger=LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/login.jspPage")
	public String jspGetLoginJsp(HttpServletRequest request){
		logger.debug(request.getRequestURI());
		logger.debug(request.getRequestURL().toString());
		logger.debug(request.getServletPath());
		logger.debug(request.getLocalAddr());
		
		return "/module/login/login";
	}
	
	@RequestMapping("/index.jspPage")
	public String jspGetIndexJsp(HttpServletRequest request){
		
		return "/module/layout/index";
	}
	
	@RequestMapping("/loginResult.json")
	@ResponseBody
	public Json jsonGetLoginResult(User user,HttpSession session) {
		Json json=userService.login(user);
		
		//2次登录，并且重新分配权限时，需要清空 并更新 sessionfo信息
		session.removeAttribute(ProUtilConfig.getSessionInfoName());
		
		if(json.isSuccess()){
			session.setAttribute(ProUtilConfig.getSessionInfoName(), json.getData());
		}
		//logger.info(json.getObject().toString());
		return json;
	}
}
